package com.item.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import com.base.dialect.PaginationSupport;
import com.base.exception.SystemException;
import com.item.dao.CityMapper;
import com.item.dao.model.City;
import com.item.dao.model.CityExample;
import com.mdx.mobile.commons.verfy.Md5;

@Service
public class CityService {
    @Autowired
    private CityMapper cityMapper;

    private static final Logger logger = Logger.getLogger(CityService.class);
    
    private static Map<String, City> cityMap;

    public int countByExample(CityExample example) {
        return this.cityMapper.countByExample(example);
    }

    public City selectByPrimaryKey(String code) {
        return this.cityMapper.selectByPrimaryKey(code);
    }

    public List<City> selectByExample(CityExample example) {
        return this.cityMapper.selectByExample(example);
    }

    public int deleteByPrimaryKey(String code) {
        int c = this.cityMapper.deleteByPrimaryKey(code);
        init();
        return  c;
    }

    public int updateByPrimaryKeySelective(City record) {
        int c = this.cityMapper.updateByPrimaryKeySelective(record);
        init();
        return  c;
    }

    public int updateByPrimaryKey(City record) {
        int c = this.cityMapper.updateByPrimaryKey(record);
        init();
        return  c;
    }

    public int deleteByExample(CityExample example) {
        int c = this.cityMapper.deleteByExample(example);
        init();
        return  c;
    }

    public int updateByExampleSelective(City record, CityExample example) {
        int c=  this.cityMapper.updateByExampleSelective(record, example);
        init();
        return  c;
    }

    public int updateByExample(City record, CityExample example) {
        int c = this.cityMapper.updateByExample(record, example);
        init();
        return  c;
    }

    public int insert(City record) {
        int c = this.cityMapper.insert(record);
        init();
        return  c;
    }

    public int insertSelective(City record) {
        int c = this.cityMapper.insertSelective(record);
        init();
        return  c;
    }
    
    private void init(){
    	if(cityMap==null){
    		cityMap = new HashMap<String, City>();
    	}
    	CityExample example = new CityExample();
    	example.setOrderByClause("code desc");
    	List<City> cities = cityMapper.selectByExample(example);
    	cityMap.clear();
    	for (City city : cities) {
    		cityMap.put(city.getCode(), city);
		}
    }
    public City getByCode(String code){
    	if(code==null||code.equals("")) return null;
    	if(cityMap==null) init();
    	return cityMap.get(code);
    }

	public String getNewCodeByLevel() {
		CityExample example = new CityExample();
		example.createCriteria().andCodeNotEqualTo("0");
		example.setOrderByClause("code desc");
		PaginationSupport.byPage(1, 1, false); 
		List<City> list = cityMapper.selectByExample(example);
		String max = null;
		if(list.size()>0)max = list.get(0).getCode();
		
		if(max == null){
			max = "100";
			return max;
		}else{
			if(Integer.parseInt(max) == 999){
				throw new SystemException("分类编号已经到达最大值,请联系开发人员！");
			}
			return Integer.parseInt(max)+1+"";
		}
	}
	
    public City login(String account, String password)throws Exception{
    	CityExample example = new CityExample();
    	example.createCriteria().andAccountEqualTo(account).andPasswordEqualTo(Md5.md5(password));
    	List<City> list = cityMapper.selectByExample(example);
    	if (list == null || list.size() == 0){
    		return null;
    	}else {
			return list.get(0);
		}
    }
    
    public int changePwd(String id, String password) throws Exception {
    	City admin = new City();
    	admin.setCode(id);
    	admin.setPassword(Md5.md5(password));
    	return cityMapper.updateByPrimaryKeySelective(admin);
    }
}